package com.swotxu.common.valid.constraint;

import com.swotxu.common.valid.ListValue;
import org.springframework.util.CollectionUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * {@link @ListValue} 自定义校验注解解析器
 *
 * @Date: 2020/12/26 20:59
 * @Author: swotXu
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
    private Set<Integer> set = new HashSet<>();
    @Override
    public void initialize(ListValue constraintAnnotation) {
        CollectionUtils.mergeArrayIntoCollection(constraintAnnotation.values(), set);
    }

    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
